<template>
  <form class="validate-form-container">
    <slot></slot>
    <div class="submit-area" @click.prevent="submitForm">
      <slot name="submit">
        <button type="submit" class="btn btn-primary">提交</button>
      </slot>
    </div>
  </form>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import mitt from 'mitt'
export const emitter = mitt()
type ValidateFunc = () => boolean
export default defineComponent({
  emits: ['form-submit'],
  setup(props, context) {
    const funcArr: ValidateFunc[] = []
    const callback = (func: ValidateFunc) => {
      funcArr.push(func)
    }
    emitter.on('form-item-created', callback)

    const submitForm = () => {
      //   循环,进行完毕，布尔数组，调用每一个方法的运行结果
      const result = funcArr.map((func) => func()).every((result) => result)
      context.emit('form-submit', result)
    }
    return { submitForm, callback }
  }
})
</script>

<style scoped></style>
